package com.example.zbh.dsliao.ui.activty;

import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.util.PathUtil;
import com.example.zbh.dsliao.R;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zbh on 15/11/28.
 */
public class ShowVideoActivity extends BaseActivity {
    private RelativeLayout loading_layout;
    private ProgressBar progressBar;
    private String localFilePath;

    @Override
    protected int getLayOut() {
        return R.layout.activity_show_video;
    }

    @Override
    protected void initView() {
        loading_layout = findeView(R.id.loading_layout);
        progressBar = findeView(R.id.progressBar);


    }

    @Override
    protected void initData() {
        localFilePath = getIntent().getStringExtra("localpath");
        String remotePath = getIntent().getStringExtra("remotepath");
        String secret = getIntent().getStringExtra("secret");

        if (localFilePath != null && new File(localFilePath).exists()) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(localFilePath)),
                    "video/mp4");
            startActivity(intent);
            finish();
        } else if (!TextUtils.isEmpty(remotePath) && !remotePath.equals("null")) {
            Map<String, String> map = new HashMap<>();
            if (!TextUtils.isEmpty(secret)) {
                map.put("share-secret", secret);
            }
            downloadVideo(remotePath, map);

        }
    }

    //下载文件
    public void downloadVideo(String remoteUrl, Map<String, String> map) {
        if (TextUtils.isEmpty(localFilePath)) {
            localFilePath = getLocalFilePath(remoteUrl);
        }
        if (new File(localFilePath).exists()) {
            showLocalVideo(localFilePath);
            return;
        }
        loading_layout.setVisibility(View.VISIBLE);
        EMCallBack emCallBack = new EMCallBack() {
            @Override
            public void onSuccess() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        loading_layout.setVisibility(View.GONE);
                        progressBar.setProgress(0);
                        showLocalVideo(localFilePath);
                    }
                });
            }

            @Override
            public void onError(int i, String s) {
                File file = new File(localFilePath);
                if (file.exists()){
                    file.delete();
                }

            }

            @Override
            public void onProgress(final int i, String s) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progressBar.setProgress(i);
                    }
                });

            }
        };
        EMChatManager.getInstance().downloadFile(remoteUrl,localFilePath,map, emCallBack);

    }

    //播放本地视频
    private void showLocalVideo(String localFilePath) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(localFilePath)),
                "video/mp4");
        startActivity(intent);
        finish();
    }

    //得到本地文件存放路径
    private String getLocalFilePath(String remoteUrl) {
        String localPath;
        if (remoteUrl.contains("/")) {
            localPath = PathUtil.getInstance().getVideoPath().getAbsolutePath()
                    + "/" + remoteUrl.substring(remoteUrl.lastIndexOf("/") + 1)
                    + ".mp4";

        } else {
            localPath = PathUtil.getInstance().getVideoPath().getAbsolutePath()
                    + "/" + remoteUrl + ".mp4";
        }
        return localPath;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}
